package com.car.carmgr.controller;

import com.car.carmgr.bean.Car;
import com.car.carmgr.bean.Customer;
import com.car.carmgr.service.CarService;
import com.car.carmgr.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("business")
public class CarController {
    
    @Autowired
    private CarService carService;

    @RequestMapping("car/carRightJson")
    public AjaxResult carRightJson(String carNumber, String carType, String color, Double price, Double rentPrice, Double deposit, Integer isRenting, String description,String carImg,Date createTime, Integer page, Integer limit){
        PageInfo<Car> pageInfo = carService.findCarByWhere(carNumber, carType,color, price, rentPrice, deposit, isRenting, description,carImg,createTime,page, limit);
        Integer count = carService.getCountByWhere(carNumber, carType,color, price, rentPrice, deposit, isRenting, description,carImg,createTime);
        return AjaxResult.ok(count,pageInfo.getList());
    }

    @RequestMapping("car/carAddOrUpdate")
    public AjaxResult carAddOrUpdate(Car car){

        int row = carService.findCarById(car.getCarNumber());//判断数据裤中是否有此车牌号
        if(row == 0){
            car.setCreateTime(new Date());
            int i = carService.insertCar(car);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else{
                return AjaxResult.ADD_FAILED();
            }
        }else{
            int i = carService.updateCar(car);
            if(i>0){
                return AjaxResult.UPD_SUCCESS();
            }else{
                return AjaxResult.UPD_FAILED();
            }
        }
    }

    @RequestMapping("car/carDelete")
    public AjaxResult carDelete(String id){
        int i = carService.deleteCarById(id);
        if(i>0){
            return AjaxResult.DEL_SUCCESS();
        }else{
            return AjaxResult.DEL_FAILED();
        }
    }

    @RequestMapping("car/deleteSomeCar")
    public AjaxResult deleteSomeCar(String carIds){
        int i = carService.deleteSomeCar(carIds.split(","));
        if(i>0){
            return AjaxResult.DELSOME_SUCCESS();
        }else{
            return AjaxResult.DELSOME_FAILED();
        }
    }
}
